<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta name="csrf-token" content="{{csrf_token()}}">
    @include('admin.public.style')
    @include('admin.public.script')
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除
                    </button>
                    <button class="layui-btn" onclick="xadmin.open('添加分类','{{url('admin/cate/create')}}',600,400)"><i
                                class="layui-icon"></i>添加
                    </button>
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>
                                <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                            </th>
                            <th>排序</th>
                            <th>类名</th>
                            <th>状态</th>
                            <th>创建时间</th>
                            <th>更新时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($permission as $v)
                            <tr>
                                <td>
                                    <input type="checkbox" name="id[]" value="{{$v->id}}" lay-skin="primary">
                                </td>
                                <td>
                                    <div class="layui-input-inline">
                                        <input type="text" name="order" onchange="changeOrder(this,{{$v->id}})" value="{{$v->order}}" style="width:30px;border:1px solid #d2d2d2;border-radius: 2px;background-color: #fff;">
                                    </div>
                                </td>
                                <td>{{$v->title}}</td>
                                <td class="td-status">
                                    <span class="layui-btn layui-btn-normal layui-btn-mini @if($v->status == 0) layui-btn-disabled  @endif">@if($v->status == 0)
                                            已停用@else已启用@endif</span>
                                </td>
                                <td>{{$v->created_at}}</td>
                                <td>{{$v->updated_at}}</td>
                                <td class="td-manage">
                                    <a onclick="member_stop(this,'{{$v->id}}')" href="javascript:;"
                                       title="@if($v->status == 0)启用@else停用@endif">
                                        <i class="layui-icon">@if($v->status == 0) &#xe62f; @else &#xe601; @endif</i>
                                    </a>
                                    <a title="编辑"
                                       onclick="xadmin.open('编辑','{{url('admin/cate/'.$v->id.'/edit')}}',600,400)"
                                       href="javascript:;">
                                        <i class="layui-icon">&#xe642; </i>
                                    </a>
                                    {{--<a onclick="xadmin.open('修改密码','{{url('admin/user/pwd')}}',600,400)" title="修改密码" href="javascript:;">--}}
                                    {{--<i class="layui-icon">&#xe631;</i>--}}
                                    {{--</a>--}}
                                    <a title="删除" onclick="member_del(this,'{{$v->id}}')" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                    </a>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate', 'form'], function () {
        var laydate = layui.laydate;
        var form = layui.form;


        // 监听全选
        form.on('checkbox(checkall)', function (data) {

            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });


    });

    /*用户-停用*/
    function member_stop(obj, id) {
        var status = $(obj).parents("tr").find(".td-status").find('span').html();
        if (status == '已停用') {
            status = '启用';
        } else {
            status = '停用';
        }
        layer.confirm('确认要' + status + '吗？', function (index) {
            if ($(obj).attr('title') == '停用') {

                //发异步把用户状态进行更改
                $(obj).attr('title', '停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');

                $.ajax({
                    type: 'PUT',
                    url: "/admin/permission/" + id,
                    dateType: 'json',
                    ContentType: 'application/x-www-form-urlencode',
                    headers: {
                        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {status: 0},
                    success: function (data) {
                        layer.msg('已停用!', {icon: 5, time: 400}, function () {
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                    }
                })
            } else {
                $.ajax({
                    type: 'PUT',
                    url: "/admin/cate/" + id,
                    dateType: 'json',
                    headers: {
                        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
                    },
                    ContentType: 'application/x-www-form-urlencode',
                    data: {status: 1},
                    success: function (data) {
                        layer.msg('已启用!', {icon: 1, time: 400}, function () {
                            // 可以对当前口进行刷新
                            location.reload();
                        });
                    }
                })
                $(obj).attr('title', '启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
            }

        });
    }

    /*用户-删除*/
    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                method: 'delete',
                url: "/admin/cate/" + id,
                dateType: 'json',
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
                },
                success: function (data) {
                    layer.msg("删除成功", {
                            icon: 1,
                            time: 400 //2秒关闭（如果不配置，默认是3秒）
                        },
                        function () {
                            // 可以对当前口进行刷新
                            location.reload();
                        });
                },
            })
        });
    }


    function delAll(argument) {
        var ids = [];
        // 获取选中的id
        $('tbody input').each(function (index, el) {
            if ($(this).prop('checked')) {
                ids.push($(this).val())
            }
        });
        layer.confirm("确认要删除吗?", function () {
            $.get('/admin/permission/delAll', {'ids': ids}, function (data) {
                console.log(data)
                layer.msg("删除成功", {icon: 1, time: 400}, function () {
                    // 可以对当前口进行刷新
                    location.reload();
                });
            });
        });

    }

    function changeOrder(obj,id){
        var order = $(obj).val();
        $.post('cate/changeOrder',{"_token":"{{csrf_token()}}","id":id,"order":order},function(data){
            if(data.status == 0){
                layer.msg(data.msg,{icon:6,time:400},function(){
                    location.reload()
                })
            }
        });
    }
</script>
</html>